package com.ruoyi.quality.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.safe.domain.BizSafeAttach;
import com.ruoyi.safe.domain.BizSafeDocAttach;
import com.ruoyi.safe.mapper.BizSafeAttachMapper;
import com.ruoyi.safe.mapper.BizSafeDocAttachMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.quality.mapper.CommonConstructionFileMapper;
import com.ruoyi.quality.domain.CommonConstructionFile;
import com.ruoyi.quality.service.ICommonConstructionFileService;

/**
 * 施工规范文件Service业务层处理
 * 
 * @author mahl2
 * @date 2024-02-27
 */
@Service
public class CommonConstructionFileServiceImpl implements ICommonConstructionFileService 
{
    @Autowired
    private CommonConstructionFileMapper commonConstructionFileMapper;
    @Autowired
    private BizSafeAttachMapper bizSafeAttachMapper;
    @Autowired
    private BizSafeDocAttachMapper bizSafeDocAttachMapper;

    @Value("${minio.bucketName}")
    private String bucketName;
    /**
     * 查询施工规范文件
     * 
     * @param id 施工规范文件主键
     * @return 施工规范文件
     */
    @Override
    public CommonConstructionFile selectCommonConstructionFileById(Long id)
    {
        CommonConstructionFile commonConstructionFile = commonConstructionFileMapper.selectCommonConstructionFileById(id);
        Set<Long> set = new HashSet<>();
        if(commonConstructionFile.getCommonConstructionFileId()!=null && !"".equals(commonConstructionFile.getCommonConstructionFileId()) ){
            if(commonConstructionFile.getCommonConstructionFileId().contains(",")){
                // 遍历数组并添加到Set中
                for (String num : commonConstructionFile.getCommonConstructionFileId().split(",")) {
                    set.add(Long.parseLong(num));
                }
            }else{
                set.add(Long.parseLong(commonConstructionFile.getCommonConstructionFileId()));
            }
            List<BizSafeDocAttach> bizSafeDocAttaches = bizSafeDocAttachMapper.selectBizSafeDocAttachByIds(set);
            commonConstructionFile.setBizSafeAttaches(bizSafeDocAttaches);
        }
        commonConstructionFile.setCommonConstructionFileId(commonConstructionFile.getCommonConstructionFileId()==""?null:commonConstructionFile.getCommonConstructionFileId());
        commonConstructionFile.setCommonConstructionName(commonConstructionFile.getCommonConstructionName()==""?null:commonConstructionFile.getCommonConstructionName());
        return  commonConstructionFile;
    }

    /**
     * 查询施工规范文件列表
     * 
     * @param commonConstructionFile 施工规范文件
     * @return 施工规范文件
     */
    @Override
    public List<CommonConstructionFile> selectCommonConstructionFileList(CommonConstructionFile commonConstructionFile)
    {
        return commonConstructionFileMapper.selectCommonConstructionFileList(commonConstructionFile);
    }

    /**
     * 新增施工规范文件
     * 
     * @param commonConstructionFile 施工规范文件
     * @return 结果
     */
    @Override
    public int insertCommonConstructionFile(CommonConstructionFile commonConstructionFile)
    {
        commonConstructionFile.setCreateTime(DateUtils.getNowDate());
        return commonConstructionFileMapper.insertCommonConstructionFile(commonConstructionFile);
    }

    /**
     * 修改施工规范文件
     * 
     * @param commonConstructionFile 施工规范文件
     * @return 结果
     */
    @Override
    public int updateCommonConstructionFile(CommonConstructionFile commonConstructionFile)
    {
        commonConstructionFile.setUpdateTime(DateUtils.getNowDate());
        return commonConstructionFileMapper.updateCommonConstructionFile(commonConstructionFile);
    }

    /**
     * 批量删除施工规范文件
     * 
     * @param ids 需要删除的施工规范文件主键
     * @return 结果
     */
    @Override
    public int deleteCommonConstructionFileByIds(Long[] ids)
    {
        return commonConstructionFileMapper.deleteCommonConstructionFileByIds(ids);
    }

    /**
     * 删除施工规范文件信息
     * 
     * @param id 施工规范文件主键
     * @return 结果
     */
    @Override
    public int deleteCommonConstructionFileById(Long id)
    {
        return commonConstructionFileMapper.deleteCommonConstructionFileById(id);
    }
}
